package org.ldh.dao.impl;


import org.ldh.dao.BookDao;
import org.ldh.domain.Book;
import org.ldh.util.ConnectionFactory;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * 图书的持久层实现类
 */
public class BookDaoImpl implements BookDao {

    @Override
    public List<Book> findAll() {
        //获取数据库连接
        Connection conn = ConnectionFactory.getConnection();
        PreparedStatement pstm = null;
        ResultSet rs = null;
        try{
            //编写sql
            String sql = "select * from book";
            //获取执行sql的对象
            pstm = conn.prepareStatement(sql);
            //执行sql
            rs = pstm.executeQuery();
            List<Book> books = new ArrayList<Book>();
            //指针默认指向表头，向下移动一行
            while(rs.next()){
                //从数据库中查询出数据，封装成实体类
                Book book = new Book();
                book.setBookId(rs.getInt("book_id"));
                book.setBookName(rs.getString("book_name"));
                book.setBookType(rs.getString("book_type"));
                book.setBookPress(rs.getString("book_press"));
                book.setBookPrice(rs.getDouble("book_price"));

                books.add(book);
            }
            return books;
        }catch (SQLException e) {
            e.printStackTrace();
        }finally {
            ConnectionFactory.close(conn,pstm,rs);
        }
        return null;
    }

    @Override
    public void saveBook(Book book) {
        //获取数据库连接
        Connection conn = ConnectionFactory.getConnection();
        PreparedStatement pstm = null;
        ResultSet rs = null;
        try{
            //编写sql
            String sql = "insert into book(book_name,book_type,book_press,book_price) values(?,?,?,?)";
            //获取执行sql的对象
            pstm = conn.prepareStatement(sql);
            //向占位符设置参数
            pstm.setString(1,book.getBookName());
            pstm.setString(2,book.getBookType());
            pstm.setString(3,book.getBookPress());
            pstm.setDouble(4,book.getBookPrice());
            //执行sql
            pstm.executeUpdate();
        }catch (SQLException e) {
            e.printStackTrace();
        }finally {
            ConnectionFactory.close(conn,pstm);
        }
    }

    @Override
    public Book findByName(String bookName) {
        //获取数据库连接
        Connection conn = ConnectionFactory.getConnection();
        PreparedStatement pstm = null;
        ResultSet rs = null;
        try{
            //编写sql
            String sql = "select * from book where book_name = ?";
            //获取执行sql的对象
            pstm = conn.prepareStatement(sql);
            //向占位符传参
            pstm.setString(1,bookName);
            //执行sql
            rs = pstm.executeQuery();
            //指针默认指向表头，向下移动一行
            while(rs.next()){
                //从数据库中查询出数据，封装成实体类
                Book book = new Book();
                book.setBookId(rs.getInt("book_id"));
                book.setBookName(rs.getString("book_name"));
                book.setBookType(rs.getString("book_type"));
                book.setBookPress(rs.getString("book_press"));
                book.setBookPrice(rs.getDouble("book_price"));

                return book;
            }
        }catch (SQLException e) {
            e.printStackTrace();
        }finally {
            ConnectionFactory.close(conn,pstm,rs);
        }
        return null;
    }

    @Override
    public Book findById(String bookId) {
        //获取数据库连接
        Connection conn = ConnectionFactory.getConnection();
        PreparedStatement pstm = null;
        ResultSet rs = null;
        try{
            //编写sql
            String sql = "select * from book where book_id = ?";
            //获取执行sql的对象
            pstm = conn.prepareStatement(sql);
            //向占位符传参
            pstm.setInt(1,Integer.parseInt(bookId));
            //执行sql
            rs = pstm.executeQuery();
            //指针默认指向表头，向下移动一行
            while(rs.next()){
                //从数据库中查询出数据，封装成实体类
                Book book = new Book();
                book.setBookId(rs.getInt("book_id"));
                book.setBookName(rs.getString("book_name"));
                book.setBookType(rs.getString("book_type"));
                book.setBookPress(rs.getString("book_press"));
                book.setBookPrice(rs.getDouble("book_price"));

                return book;
            }
        }catch (SQLException e) {
            e.printStackTrace();
        }finally {
            ConnectionFactory.close(conn,pstm,rs);
        }
        return null;
    }

    @Override
    public void update(Book book) {
        //获取数据库连接
        Connection conn = ConnectionFactory.getConnection();
        PreparedStatement pstm = null;
        ResultSet rs = null;
        try{
            //编写sql
            String sql = "update book set book_name = ? , book_type = ? , book_press = ? , book_price = ? where book_id = ?";
            //获取执行sql的对象
            pstm = conn.prepareStatement(sql);
            //向占位符传参
            pstm.setString(1,book.getBookName());
            pstm.setString(2,book.getBookType());
            pstm.setString(3,book.getBookPress());
            pstm.setDouble(4,book.getBookPrice());
            pstm.setInt(5,book.getBookId());
            //执行sql
            pstm.executeUpdate();
        }catch (SQLException e) {
            e.printStackTrace();
        }finally {
            ConnectionFactory.close(conn,pstm,rs);
        }
    }

    @Override
    public void deleteById(String bookId) {
        //获取数据库连接
        Connection conn = ConnectionFactory.getConnection();
        PreparedStatement pstm = null;
        ResultSet rs = null;
        try{
            //编写sql
            String sql = "delete from book where book_id = ?";
            //获取执行sql的对象
            pstm = conn.prepareStatement(sql);
            //向占位符传参
            pstm.setInt(1,Integer.parseInt(bookId));
            //执行sql
            pstm.executeUpdate();
        }catch (SQLException e) {
            e.printStackTrace();
        }finally {
            ConnectionFactory.close(conn,pstm,rs);
        }
    }
}
